package com.hou.service;

import com.hou.dao.PlaceDao;
import com.hou.dao.ProvinceDao;
import com.hou.pojo.Place;
import com.hou.pojo.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shkstart
 * @create 2021-09-22-22:10
 */
@Service
public class PlaceServiceImpl implements PlaceService{
    @Autowired
    private PlaceDao placeDao;
    @Autowired
    private ProvinceService provinceService;

    @Override
    public List<Place> findByProvinceIdPage(Integer page, Integer rows, String provinceId) {
        int start = (page - 1) * rows;
        return placeDao.findByProvinceIdPage(start,rows,provinceId);
    }

    @Override
    public Integer findByProvinceIdCounts(String provinceId) {
        return placeDao.findByProvinceIdCounts(provinceId);
    }

    @Override
    public void save(Place place) {
        //保存景点
        placeDao.save(place);
        //查询省份id
        Province province = provinceService.findProvinceById(Integer.parseInt(place.getProvince()));
        System.out.println(province);
        //更新省份景点个数
        province.setPlacecounts(province.getPlacecounts()+1);
        provinceService.updateProvince(province);
    }

    @Override
    public void delete(Integer id) {
        // 获取景点信息
        Place place = placeDao.getPlaceById(id.toString());
        // 省份id
        String province = place.getProvince();
        // 省份信息
        Province provinceById = provinceService.findProvinceById(Integer.parseInt(province));
        // 景点数目-1
        provinceById.setPlacecounts(provinceById.getPlacecounts() - 1);
        // 更新
        provinceService.updateProvince(provinceById);
        // 删除景点
        placeDao.delete(id.toString());
    }

    // 获取单条景点信息
    @Override
    public Place getPlaceById(Integer id) {
        Place place = placeDao.getPlaceById(id.toString());
        return place;
    }

    @Override
    public void updatePlace(Place place) {
        placeDao.update(place);
    }
}
